﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeChatParking.Core.BLL
{
    public class Device
    {
        public Entity.Device Get(int ID)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                try
                {
                    return dbContext.Get<Entity.Device>(ID, null);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex, "获取车辆记录");
                    return null;
                }
            }
        }
        public Entity.Device Get(string Client_code)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { DeviceClient_code = Client_code };
                return dbContext.GetList<Entity.Device>(precidate, null).FirstOrDefault();
            }

        }
        public string GetDeviceClient_code(string PayApiKey)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { Api_secret = PayApiKey };
                var de =dbContext.GetList<Entity.Device>(precidate, null).FirstOrDefault();
                return de == null ? null : de.DeviceClient_code;
            }

        }
        public  List<Entity.Device> GetList(string Client_code)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { DeviceClient_code = Client_code };
                return dbContext.GetList<Entity.Device>(precidate, null).ToList();
            }

        }
        public Entity.Device GetToName(string Name)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { Device_name = Name };
                return dbContext.GetList<Entity.Device>(precidate, null).FirstOrDefault();
            }

        }
        public string Update(Entity.Device de, string openuser)
        {

            var model = this.Get(de.ID);
            model.Device_IP = de.Device_IP;
            model.Device_name = de.Device_name;
            model.create_time = DateTime.Now;
            model.update_user = openuser;
            model.update_time = DateTime.Now;
            var bll = new Core.BLL.Enterprise();
            var ent = bll.Get(de.Enterprise_ID);
            model.Enterprise_ID = de.Enterprise_ID;
            model.Enterprise_name = ent.Enterprise_Name;
            var Tcent = bll.Get(de.TcEnterprise_ID);
            model.TcEnterprise_ID = de.TcEnterprise_ID;
            model.TcEnterprise_name = Tcent.Enterprise_Name;
            model.Api_secret = de.Api_secret;
            model.version = de.version;
            using (var dbContext = Repository.DbContext.Create())
            {
                if (dbContext.Update(model))
                    return "";
                else
                    return "失败";
            }
        }
        public int Add(Entity.Device de, string openuser)
        {
            int newId = -1;
            if (this.ExistsName(de.Device_name))
                return newId;
            //if (this.ExistsCode(de.Device_code))
            //    return newId;
            de.Device_IP = "";
            de.create_user = openuser;
            de.update_user = openuser;
            de.create_time = DateTime.Now;
            de.update_time = DateTime.Now;
            de.version = 0;
            var bll = new Core.BLL.Enterprise();
            var ent = bll.Get(de.Enterprise_ID);
            de.Enterprise_name = ent.Enterprise_Name;
            var Tcent = bll.Get(de.TcEnterprise_ID);
            de.TcEnterprise_name = Tcent.Enterprise_Name;
            using (var dbContext = Repository.DbContext.Create())
                newId = dbContext.Insert(de);

            return newId;
        }
      
     
        /// <summary>
        /// 是否存在名
        /// </summary>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public bool ExistsName(string Device_name)
        {
            using (var dbContext = Repository.DbContext.Create())
            {
                var precidate = new { Device_name = Device_name };
                return dbContext.Count<Entity.Device>(precidate, null) > 0;
            }
        }

        /// <summary>
        ///删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public string Delete(int Id)
        {

            using (var dbContext = Repository.DbContext.Create())
            {
                var predicate = new { ID = Id };
                bool success = dbContext.Delete<Entity.Device>(predicate, null);
                if (success)
                    return string.Empty;
                return "删除失败";
            }
        }
    }
}
